package org.example.common.core.utils;

import org.apache.commons.lang3.time.DateFormatUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.Objects;

/**
 * 时间工具类
 *
 * @author royal
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
    public static String YYYY = "yyyy";
    public static String YYYY_MM = "yyyy-MM";
    public static String YYYY_MM_DD = "yyyy-MM-dd";
    public static String YYYY_MM_DD_HH = "yyyy-MM-dd HH";
    public static String YYYYMMDD = "yyyyMMdd";
    public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    public static String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
    public static String MMDDHHMMSS = "MMddHHmmss";
    public static String HH = "HH";
    public static String HH_MM = "HH:mm";
    public static String HH_MM_SS = "HH:mm:ss";
    public static String HHMMSS = "HHmmss";

    public static String[] parsePatterns = {
            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};

    public static String START_TIME_STRING = "00:00:00";

    public static String END_TIME_STRING = "23:59:59";


    /**
     * 获取两个时间分钟差
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static Integer getDateMin(String startDate, String endDate) {
        SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date begin = null;
        Date end = null;
        try {
            begin = dfs.parse(startDate);
            end = dfs.parse(endDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long between = (end.getTime() - begin.getTime()) / 1000;
        long min = between / 60;
        return Integer.parseInt(min + "");
    }

    /**
     * 计算两个时间差
     */
    public static Integer getDateMin(Date startDate, Date endDate) {
        long min = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - startDate.getTime();

        // 计算差多少小时
        Long totalMin = diff / min;
        if (diff % min != 0) {
            totalMin = totalMin + 1;
        }
        return totalMin.intValue();
    }

    /**
     * 获取两个时间秒差
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static Integer getDateSeconds(String startDate, String endDate) {
        SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date begin = null;
        Date end = null;
        try {
            begin = dfs.parse(startDate);
            end = dfs.parse(endDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long between = (end.getTime() - begin.getTime()) / 1000;
        return Integer.parseInt(between + "");
    }

    /**
     * 获取今日结束时间
     *
     * @return
     */
    public static String getTodayEnd() {
        SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD);
        return (sdf.format(new Date()) + " " + END_TIME_STRING);
    }

    /**
     * 获取指定分钟
     *
     * @return
     */
    public static Date getAfterMinute(Date date, int minute) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MINUTE, minute);
        return cal.getTime();
    }


    /**
     * 获取指定秒数
     *
     * @return
     */
    public static Date getAfterSecond(Date date, int second) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.SECOND, second);
        return cal.getTime();
    }

    /**
     * 获取当前Date型日期
     *
     * @return Date() 当前日期
     */
    public static Date getNowDate() {

        return new Date();
    }

    /**
     * 获取当前日期, 默认格式为yyyy-MM-dd
     *
     * @return String
     */
    public static String getDate() {
        return dateTimeNow(YYYY_MM_DD);
    }

    public static final String getTime() {
        return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
    }

    public static final String dateTimeNow(final String format) {
        return parseDateToStr(format, new Date());
    }

    public static final String dateTimeNow() {
        return dateTimeNow(YYYYMMDDHHMMSS);
    }

    public static final String dateTime(final Date date) {
        return parseDateToStr(YYYY_MM_DD, date);
    }

    public static final String parseDateToStr(final String format, final Date date) {
        return new SimpleDateFormat(format).format(date);
    }

    public static final Date dateTime(final String format, final String ts) {
        try {
            return new SimpleDateFormat(format).parse(ts);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 日期路径 即年/月/日 如20180808
     */
    public static final String dateTime() {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyyMMdd");
    }

    /**
     * 日期型字符串转化为日期 格式
     */
    public static Date parseDate(Object str) {
        if (str == null) {
            return null;
        }
        try {
            return parseDate(str.toString(), parsePatterns);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * 获取时分秒
     *
     * @param date
     * @return
     */
    public static Date getDate(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.getTime();
    }

    /**
     * 获取指定格式时间
     *
     * @param date
     * @param format
     * @return
     */
    public static Date getDate(Date date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return dateTime(format, sdf.format(date));
    }

    public static String toDateTime(LocalDateTime date) {
        return toDateTime(date, YYYY_MM_DD_HH_MM_SS);
    }

    public static String toDateTime(LocalDateTime dateTime, String pattern) {
        return dateTime.format(DateTimeFormatter.ofPattern(pattern, Locale.SIMPLIFIED_CHINESE));
    }

    /***
     * 获取当前日期, 默认格式为yyyyMMdd
     * @return
     */
    public static String getClearDate() {
        return DateUtils.parseDateToStr(DateUtils.YYYYMMDD, new Date());
    }

    /***
     * 获取当前日期, 默认格式为yyyyMMdd
     * @param time 时间（非必填）
     * @return
     */
    public static String getClearDate(String time) {
        if (Objects.nonNull(time)) {
            Date date = DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS, time);
            return DateUtils.parseDateToStr(DateUtils.YYYYMMDD, date);
        }
        return dateTimeNow(YYYYMMDD);
    }

    /**
     * 时间戳转日期
     *
     * @param timeStamp
     * @return
     */
    public static Date TimeStampToDate(long timeStamp) {
        Date date = new Date(timeStamp);
        return date;
    }

    /**
     * 日期路径 即年/月/日 如2018/08/08
     */
    public static String datePath() {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyy/MM/dd");
    }

}
